var express = require('express');
var router = express.Router();
var crypto = require('crypto');

var db = require("./../../utils/db.js");

// 获取模块列表
router.get('/listAll', function (req, res, next) {
    res.render('module/listAll');
});

// 获取模块列表（vis使用，包含关联关系）
router.get('/moduleData', function (req, res, next) {
    var moduleArray = [];
    var moduleContact = [];

    var sql = "select * from module_base where 1=1";
    var contactSql = "select * from module_relition where 1=1";

    db.query(sql, function (err, rows) {
        for (var i = 0; i < rows.length; i++) {
            var modules = {id: rows[i].id, label: rows[i].module_name};
            moduleArray.push(modules);
        }

        db.query(contactSql, function (err, rows) {
            for (var i = 0; i < rows.length; i++) {
                var modules = {from: rows[i].module_own_id, to: rows[i].module_rely_id, arrows: 'to'};
                moduleContact.push(modules);
            }

            if (rows && rows.length >= 0) {
                res.json({moduleArray: moduleArray, moduleContact: moduleContact});
            } else {
                res.render('error', {
                    message: err.message,
                    error: err
                });
            }
        });


    });


});

// 获取模块列表
router.get('/moduleList', function (req, res, next) {
    var moduleList = [];

    var sql = "select * from module_base where 1=1";

    db.query(sql, function (err, rows) {
        for (var i = 0; i < rows.length; i++) {
            var modules = {id: rows[i].id, text: rows[i].module_name};
            moduleList.push(modules);
        }

        if (rows && rows.length >= 0) {
            res.json({
                results: moduleList
            });
        } else {
            res.render('error', {
                message: err.message,
                error: err
            });
        }

    });


});

//模块添加
router.get("/add", function (req, res, next) {
    res.render('module/edit', {
        module: {
            id: '',
            moduleName: '',
            moduleDescription: ''
        },
        action: 'save'
    });
});

//模块保存
router.post('/save', function (req, res, next) {
    var moduleName = req.body.moduleName;
    var moduleDescription = req.body.moduleDescription;
    var dependencies = req.body.dependencies;

    var loginedUser = req.session.loginedUser;
    var moduleCreatPerson = loginedUser.id;
    var moduleCreatTime = moment().format('YYYY-MM-DD HH:mm:ss');

    var sql = "insert into module_base(module_name,module_description,module_creat_person,module_creat_time) values('" + moduleName + "','" + moduleDescription + "','" + moduleCreatPerson + "','" + moduleCreatTime + "')";

    db.query(sql, function (err, rows) {
        if (!err) {
            res.json({
                errcode: 0,
                message: '保存成功'
            });
        } else {
            res.json({
                errcode: 1,
                message: '保存失败'
            });

        }
    });

});
//模块修改
router.get("/edit/:id", function (req, res, next) {
    var id = req.params.id;
    var sql = "select * from module_base where id =  '" + id + "'";

    db.query(sql, function (err, rows) {

        if (err) {
            res.send("修改页面跳转失败");
        } else {
            res.render('module/edit', {
                module: {
                    id: rows[0].id,
                    moduleName: rows[0].module_name,
                    moduleDescription: rows[0].module_description,
                    moduleCreatPerson: rows[0].module_creat_person,
                    moduleCreatTime: rows[0].module_creat_time
                },
                action: 'update'
            });
        }
    });
});

router.post("/update", function (req, res, next) {
    var id = req.body.id;
    var moduleName = req.body.moduleName;
    var moduleDescription = req.body.moduleDescription;

    var loginedUser = req.session.loginedUser;
    var moduleCreatPerson = loginedUser.id;
    var moduleCreatTime = moment().format('YYYY-MM-DD HH:mm:ss');

    var sql = "update module_base set module_name = '" + moduleName + "', module_description = '" + moduleDescription + "',module_creat_person = '" + moduleCreatPerson + "',module_creat_time = '" + moduleCreatTime + "' where id = " + id;
    db.query(sql, function (err, rows) {
        if (!err) {
            res.json({
                errcode: 0,
                message: '修改成功'
            });
        } else {
            res.json({
                errcode: 1,
                message: '修改失败'
            });

        }
    });
});

//模块查看版本信息
router.get("/detail/:id", function (req, res, next) {
    var id = req.params.id;
    var sql = "select * from version_base where module_base_id =  '" + id + "'";
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.render('module/version/index', {
                versionList: rows
            });
        } else {
            res.render('error', {
                message: err.message,
                error: err
            });
        }

    });
});


//模块删除
router.get("/delete/:id", function (req, res) {
    var id = req.params.id;
    var sql = "delete from module_base where id =  '" + id + "'";
    db.query(sql, function (err, rows) {
        if (err) {
            res.send("删除失败" + err);
        }
        else {
            res.redirect("/module/listAll");
        }
    });
});


//模块查询
router.post("/search", function (req, res, next) {
    var moduleName = req.body.moduleName;
    var moduleCreatPerson = req.body.moduleCreatPerson;
    var sql = "select * from module_base where 1=1 ";
    if (moduleName) {
        sql += "and module_name like '%" + moduleName + "%'";
    }
    if (moduleCreatPerson) {
        sql += "and module_creat_person like '%" + moduleCreatPerson + "%'";
    }
    db.query(sql, function (err, rows) {
        if (err) {
            res.send("查询失败: " + err);
        } else {
            res.render('module/listAll', {
                title: "模块列表",
                moduleList: rows,
                moduleName: moduleName,
                moduleCreatPerson: moduleCreatPerson
            });
        }
    });
});
module.exports = router;